//
//  YH_WebViewController.m
//  YinHang
//
//  Created by 张亚哲 on 14/11/1.
//  Copyright (c) 2014年 孙彦鹏. All rights reserved.
//

#import "YH_WebViewController.h"

@interface YH_WebViewController ()<UIWebViewDelegate,UIAlertViewDelegate>{
    
    __weak IBOutlet UIWebView *WebView;
    NSString *rurl;
    NSString *state;
}

@end

@implementation YH_WebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    state = @"first";
    self.navigationItem.title = @"预览";
    
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = - 10;
    if (ios7) {
        self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftBarButtonItem];
    }else{
        self.navigationItem.leftBarButtonItems = @[leftBarButtonItem];
    }
    //_url = @"www.baidu.com";
    WebView.delegate = self;
    [self openUrl];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

#pragma mark Custom Method

-(void)back{
    if ([state isEqualToString:@"first"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [WebView goBack];
    }
}

-(void)openUrl{
    if (!_url) {
        
    }else{
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_url]];
       // NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:_url]cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];
        [WebView loadRequest:request];
    }
}

#pragma mark - webview

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    //[SVProgressHUD showErrorWithStatus:[error description]];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"Loading...";
}

- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType{
    rurl = [[request URL] absoluteString];
    if ([rurl hasPrefix:@"tel://"]) {
        //如果是电话请求, 再截取协议中的方法和参数, 判断无误后在这里手动调用oc方法
        NSLog(@"%@",rurl);
        NSString *phoneNumber = [NSString stringWithFormat:@"是否拨打号码：%@",[rurl substringFromIndex:6]];
        //打电话
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:phoneNumber delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        
    }
    if ([rurl isEqualToString:_url]) {
        state = @"first";
    }else{
        state = @"notFirst";
    }
    return YES;
}
-(void)dealloc{
    NSLog(@"webView dealloc");
    rurl = nil;
    state = nil;
}
//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//    return YES;
//}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:rurl]];
    }
}
@end
